package com.acme.service.bank.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import com.acme.domain.account.Account;
import com.acme.domain.account.AccountType;
import com.acme.domain.account.DepositComparator;
import com.acme.domain.client.Bank;
import com.acme.domain.client.Client;
import com.acme.service.bank.BankReportService;

public class BankReportServiceImpl implements BankReportService{

	@Override
	public int getNumberOfCustomers(Bank bank) {
		return bank.getClientsList().size();
	}

	@Override
	public int getNumberOfAccounts(Bank bank) {
		int numberOfAccounts = 0;
		for (Client cli : bank.getClientsList()) {
			numberOfAccounts += cli.getAccountsList().size();
		}
		return numberOfAccounts;
	}

	@Override
	public double getBankCredit(Bank bank) {
		double bankCredit = 0;
		for (Client cli : bank.getClientsList()) {
			for (Account acc : cli.getAccountsList()) {
				if (acc.getAccountType().equals(AccountType.CHECKING)
						&& acc.getBalance() == 0) {
					bankCredit += acc.getOverdraft();
				}
			}
		}
		return bankCredit;
	}

	@Override
	public Set<Account> getAccountsSortedBySum(Bank bank) {
		Set<Account> sortedAccounts = new TreeSet<Account>(new DepositComparator());
		for (Client cli : bank.getClientsList()) {
			sortedAccounts.addAll(cli.getAccountsList());
		}
		return sortedAccounts;
	}

	@Override
	public Map<String, Set<Account>> getCustomerAccounts(Bank bank) {
		Map<String, Set<Account>> map = new HashMap<String, Set<Account>>();
		for (Client cli : bank.getClientsList()) {
			map.put("\n"+cli.getSalutation(), cli.getAccountsList());
		}
		return map;

	}


}